<template>
  <div id="SupplierRv">
    <el-row>
      <el-col :span="6">
        <label>审核状态</label>
        <el-select v-model="selectedCheckTag" placeholder="请选择审核状态" @change="onSubmit">
          <el-option label="等待审核" value="S001-1"></el-option>
          <el-option label="审核未通过" value="S001-2"></el-option>
          <el-option label="审核通过" value="S001-3"></el-option>
        </el-select>
      </el-col>
    </el-row>
    <el-table :data="tableData" style="width: 100%;text-align: center;padding-left: 70px">
      <el-table-column prop="supplierId" label="编号" width="80"></el-table-column>
      <el-table-column prop="nickName" label="申请用户" width="150"></el-table-column>
      <el-table-column prop="licenseLink" label="营业执照" width="120">
        <template slot-scope="scope">
          <img :src="'/img'+scope.row.licenseLink" alt="营业执照" style="max-width: 50px; max-height: 50px;">
        </template>
      </el-table-column>
      <el-table-column prop="companyName" label="公司名称" width="200"></el-table-column>
      <el-table-column prop="checkTag" label="审核状态" width="150">
        <template slot-scope="scope">
          {{ getCheckTagText(scope.row.checkTag) }}
        </template>
      </el-table-column>
      <el-table-column prop="createDate" label="申请时间" width="200"></el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button
              :type="scope.row.checkTag === 'S001-3'? 'text' : 'primary'"
              :disabled="scope.row.checkTag === 'S001-3'"
              :icon="scope.row.checkTag === 'S001-3'? 'el-icon-check' : 'el-icon-edit'"
              :class="scope.row.checkTag === 'S001-3'? 'disabled-button' : ''"
              @click="openReviewDialog(scope.row)">
            {{ scope.row.checkTag === 'S001-3'? '审核通过' : '审核' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        :total="total"
        :page-size="pageSize"
        :current-page="currentPage"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange">
    </el-pagination>
    <el-dialog
        title="审核供货商"
        :visible.sync="reviewDialogVisible"
        width="400px"
        :before-close="handleClose">
      <el-form :model="reviewForm">
        <el-form-item label="审核理由">
          <el-input v-model="reviewForm.reason" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="审核结果">
          <el-radio-group v-model="reviewForm.result">
            <el-radio label="S001-2">审核未通过</el-radio>
            <el-radio label="S001-3">审核通过</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
                <span class="dialog-footer">
                    <el-button @click="handleClose">取消</el-button>
                    <el-button type="primary" @click="submitReview">确定</el-button>
                </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "SupplierRv",
  data() {
    return {
      supplier: {
        user: '',
        review: ''
      },
      tableData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      selectedCheckTag: '',
      reviewDialogVisible: false,
      reviewForm: {
        reason: '',
        result: ''
      },
      currentSupplier: {}
    };
  },
  methods: {
    onSubmit() {
      this.fetchPendingAndRejectedSupplierList();
    },
    refresh() {
      this.supplier.user = '';
      this.supplier.review = '';
      this.selectedCheckTag = '';
      this.fetchPendingAndRejectedSupplierList();
    },
    fetchPendingAndRejectedSupplierList() {
      this.$axios.get(`http://localhost:8090/supplier/pendingAndRejectedList`, {
        params: {
          companyName: this.supplier.user,
          page: this.currentPage,
          pageSize: this.pageSize,
          checkTag: this.selectedCheckTag
        }
      }).then(response => {
        this.tableData = response.data.list;
        this.total = response.data.total;
        this.tableData.forEach(item => {
          console.log('checkTag:', item.checkTag);
        });
      }).catch(error => {
        console.error(error);
      });
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.currentPage = 1;
      this.fetchPendingAndRejectedSupplierList();
    },
    handleCurrentChange(newPage) {
      this.currentPage = newPage;
      this.fetchPendingAndRejectedSupplierList();
    },
    getCheckTagText(checkTag) {
      switch (checkTag) {
        case 'S001-1':
          return '等待审核';
        case 'S001-2':
          return '审核未通过';
        case 'S001-3':
          return '审核通过';
        default:
          return '';
      }
    },
    openReviewDialog(supplier) {
      this.currentSupplier = supplier;
      this.reviewDialogVisible = true;
      this.reviewForm.reason = '';
      this.reviewForm.result = '';
    },
    handleClose() {
      this.reviewDialogVisible = false;
    },
    submitReview() {
      // 验证是否选择了审核结果
      if (!this.reviewForm.result) {
        this.$message.error('请选择审核结果');
        return;
      }

      this.$axios.post(`http://localhost:8090/supplier/review`, {
        supplierId: this.currentSupplier.supplierId,
        reason: this.reviewForm.reason,
        result: this.reviewForm.result
      }).then(response => {
        if (response.data.success) {
          this.reviewDialogVisible = false;
          this.fetchPendingAndRejectedSupplierList();
        } else {
          this.$message.error('审核失败');
        }
      }).catch(error => {
        console.error(error);
        this.$message.error('审核失败');
      });
    }
  },
  created() {
    this.fetchPendingAndRejectedSupplierList();
  },
};
</script>

<style scoped>
.pagination {
  margin-top: 20px;
  text-align: right;
}

.disabled-button {
  cursor: not-allowed;
  color: #ccc;
}
</style>
